package com.stylefeng.guns.modular.user_buy_device.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.stylefeng.guns.core.base.controller.BaseController;
import com.stylefeng.guns.modular.system.model.User;
import com.stylefeng.guns.modular.system.service.IUserService;
import com.stylefeng.guns.modular.user_buy_device.model.UserBuyDeviceDO;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.beans.factory.annotation.Autowired;
import com.stylefeng.guns.core.log.LogObjectHolder;
import org.springframework.web.bind.annotation.RequestParam;
import com.stylefeng.guns.modular.user_buy_device.model.UserBuyDeviceInfo;
import com.stylefeng.guns.modular.user_buy_device.service.IUserBuyDeviceInfoService;

import javax.annotation.Resource;

/**
 * 商户设备管理控制器
 *
 * @author fengshuonan
 * @Date 2020-07-20 19:43:16
 */
@Controller
@RequestMapping("/userBuyDeviceInfo")
public class UserBuyDeviceInfoController extends BaseController {

    private String PREFIX = "/user_buy_device/userBuyDeviceInfo/";

    @Autowired
    private IUserBuyDeviceInfoService userBuyDeviceInfoService;

    @Resource
    private IUserService iUserService;

    /**
     * 跳转到商户设备管理首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "userBuyDeviceInfo.html";
    }

    /**
     * 跳转到添加商户设备管理
     */
    @RequestMapping("/userBuyDeviceInfo_add")
    public String userBuyDeviceInfoAdd() {
        return PREFIX + "userBuyDeviceInfo_add.html";
    }

    /**
     * 跳转到修改商户设备管理
     */
    @RequestMapping("/userBuyDeviceInfo_update/{userBuyDeviceInfoId}")
    public String userBuyDeviceInfoUpdate(@PathVariable Integer userBuyDeviceInfoId, Model model) {
        UserBuyDeviceDO userBuyDeviceDO = userBuyDeviceInfoService.getOneUserDevice(userBuyDeviceInfoId);
        model.addAttribute("item",userBuyDeviceDO);
        LogObjectHolder.me().set(userBuyDeviceDO);
        return PREFIX + "userBuyDeviceInfo_edit.html";
    }

    /**
     * 获取商户设备管理列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(@RequestParam(value = "page",defaultValue = "0") Integer page,
                       @RequestParam(value = "limit",defaultValue = "20") Integer limit) {
        return userBuyDeviceInfoService.getBuyDeviceList(page,limit);
    }

    /**
     * 新增商户设备管理
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    @Transactional
    public Object add(UserBuyDeviceInfo userBuyDeviceInfo) {
        return userBuyDeviceInfoService.buyDevice(userBuyDeviceInfo);
    }

    /**
     * 修改商户设备管理
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    @Transactional
    public Object update(UserBuyDeviceInfo userBuyDeviceInfo) {
        userBuyDeviceInfoService.updateById(userBuyDeviceInfo);
        return SUCCESS_TIP;
    }

    @RequestMapping(value = "/getUserList")
    @ResponseBody
    public Object getUserList(){
        EntityWrapper<User> wrapper = new EntityWrapper<>();
        wrapper.notIn("roleid","1");
        wrapper.setSqlSelect("id","name");
        return iUserService.selectList(wrapper);
    }

    /**
     * 查询可购买设备数
     * @return
     */
    @RequestMapping(value = "/getEnableBuyCount")
    @ResponseBody
    public Object getEnableBuyCount(){
        return userBuyDeviceInfoService.getEnableBuyCount();
    }
}
